package com.adtec.base.cloud.baseservice.rbac.system.controller;


import com.adtec.base.cloud.baseservice.rbac.system.model.SysOrgNode;
import com.adtec.base.cloud.baseservice.rbac.system.service.SysOrgService;
import com.adtec.base.cloud.baseservice.rbac.system.service.SysUserService;
import com.adtec.base.cloud.commons.exception.AjaxResponse;
import com.adtec.base.cloud.persistence.system.model.SysOrg;
import com.adtec.base.cloud.persistence.system.model.SysUser;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/sysorg")
@Tag(name = "sysorg", description = "组织管理")
public class SysOrgController {

    @Resource
    private SysOrgService sysorgService;
    @Resource
    private SysUserService sysuserService;

    @PostMapping(value = "/tree")
    @Operation(description = "组织管理：查询")
    @Parameters({
            @Parameter(name = "username", description = "用户名根据当前用户名查询关联的组织和下级组织"),
            @Parameter(name = "orgNameLike", description = "组织名称(模糊查询)"),
            @Parameter(name = "orgStatus", description = "组织状态"),
    })
    public List<SysOrgNode> tree(@RequestParam("username") String username,
                                 @RequestParam(name = "orgNameLike", required = false) String orgNameLike,
                                 @RequestParam(name = "orgStatus", required = false) Boolean orgStatus) {
        SysUser sysUser = sysuserService.getUserByUserName(username);
        return sysorgService.getOrgTreeById(sysUser.getOrgId(), orgNameLike, orgStatus);
    }

    @PostMapping(value = "/update")
    @Operation(description = "组织管理：更新")
    public AjaxResponse update(@RequestBody SysOrg sysOrg) {
        sysorgService.updateOrg(sysOrg);
        return AjaxResponse.success("更新组织机构成功！");
    }

    @PostMapping(value = "/add")
    @Operation(description = "组织管理：新增")
    public AjaxResponse add(@RequestBody SysOrg sysOrg) {
        sysorgService.addOrg(sysOrg);
        return AjaxResponse.success("新增组织机构成功！");
    }


    @PostMapping(value = "/delete")
    @Operation(description = "组织管理：删除")
    public AjaxResponse delete(@RequestBody SysOrg sysOrg) {
        sysorgService.deleteOrg(sysOrg);
        return AjaxResponse.success("删除组织机构成功!");
    }

    //组织管理：更新组织禁用状态
    @PostMapping(value = "/status/change")
    @Operation(description = "组织管理：更新组织禁用状态")
    @Parameters({
            @Parameter(name = "orgId", description = "组织ID"),
            @Parameter(name = "status", description = "组织状态"),
    })
    public AjaxResponse update(@RequestParam Long orgId,
                               @RequestParam Boolean status) {
        sysorgService.updateStatus(orgId, status);
        return AjaxResponse.success("组织禁用状态更新成功！");
    }
}
